package com.app.project;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

public class BESM_SplashActivity extends Activity {
	
	public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
	private ProgressDialog mProgressDialog;
	
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		
		new  Loading().execute();
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Loading.....");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setCancelable(true);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
        }
    }
	
	 // Download Image in Background
    public class Loading extends AsyncTask<String, Void, Void> {
    		
		protected void onPreExecute() {
        	
        	super.onPreExecute();
        	//
        	new Thread() {
    			public void run() {
    				ImageView select=(ImageView)findViewById(R.id.A_Main_imageuser);
    		        Animation mAnimation = new AlphaAnimation(1, 0);
    		        mAnimation.setDuration(1000);
    		        mAnimation.setInterpolator(new LinearInterpolator());
    		        mAnimation.setRepeatCount(Animation.INFINITE);
    		        mAnimation.setRepeatMode(Animation.REVERSE); 
    		        select.startAnimation(mAnimation);
//    		        try {
//    					sleep(2000);
//    					
//    				} catch (InterruptedException e) {
//    					// TODO Auto-generated catch block
//    					e.printStackTrace();
//    				}
//    		        new  Loading().execute();
    			};
    			
    		}.start();
    		
        }

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
        	SystemClock.sleep(2000);
        	Intent newActivity = new Intent(BESM_SplashActivity.this,BESM_LoginActivity.class);
			startActivity(newActivity);
    		return null;
        }

		protected void onPostExecute(Void unused) {
			ImageView select=(ImageView)findViewById(R.id.A_Main_imageuser);
			select.clearAnimation();
        	finish();
//            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//            removeDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
        
		 
    }
    
	@Override
	public void onBackPressed() {
	  
	        new AlertDialog.Builder(this)
	        .setTitle("ออกจากโปรแกรม")
	        .setMessage("ต้องการออกจากโปรแกรม")
	                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                        finish();
	                        System.exit(0);
	                    }
	                })
	                .setPositiveButton(android.R.string.no, null).create().show();
	} 
	
	
}
